package tpsi.pong;

public class Paddle {
	private int x, y;
	private final int width, height;
	private int speed;
	private PaddleMove paddleMove;
	private PaddleColor paddleColor;

	public Paddle() {
		this.setPaddleMove(PaddleMove.NONE);
		this.width = 10;
		this.height = 70;
		this.setSpeed(5);
		this.setLocation(0, 0);
		this.setPaddleColor(PaddleColor.WHITE);
	}

	public Paddle(PaddleColor pc) {
		this.setPaddleMove(PaddleMove.NONE);
		this.width = 10;
		this.height = 70;
		this.setSpeed(5);
		this.setLocation(0, 0);
		this.setPaddleColor(pc);
	}

	public void setY(int y) {
		if (y >= 0)
			this.y = y;
		else
			this.y = 0;
	}

	public int getY() {
		return y;
	}

	public void setX(int x) {
		if (x >= 0)
			this.x = x;
		else
			this.x = 0;
	}

	public int getX() {
		return x;
	}

	public void setLocation(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public void setPaddleMove(PaddleMove padleMove) {
		this.paddleMove = padleMove;
	}

	public PaddleMove getPadleMove() {
		return paddleMove;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public int getSpeed() {
		return speed;
	}

	public int getHeight() {
		return height;
	}

	public int getWidth() {
		return width;
	}

	/**
	 * Moves Paddle by this speed in this paddleMove direction.
	 */
	public void move() {
		switch (this.paddleMove) {
		case UP:
			this.setY(this.y - this.speed);
			break;
		case DOWN:
			this.setY(this.y + this.speed);
			break;
		default:
			break;
		}
	}

	/**
	 * Builds and return PPOS message.
	 * 
	 * @return String replay message.
	 */
	public String getPPOSMsg() {
		StringBuilder sb = new StringBuilder();
		sb.append("PPOS\n");
		sb.append(String.valueOf(this.x));
		sb.append(":");
		sb.append(String.valueOf(this.y));
		return sb.toString();
	}

	/**
	 * Builds and return PMOVE message and PPOS message.
	 * 
	 * @return String replay message.
	 */
	public String getPMOVEMsg() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.getPPOSMsg());
		sb.append("\n");
		sb.append("PMOVE\n");
		sb.append(this.paddleMove.toString());
		return sb.toString();
	}

	public void setPaddleColor(PaddleColor paddleColor) {
		this.paddleColor = paddleColor;
	}

	public PaddleColor getPaddleColor() {
		return paddleColor;
	}

	public void setPaddlePos(String msg) throws NumberFormatException {
		String[] pos = msg.split(":");
		this.setLocation(Integer.parseInt(pos[0]), Integer.parseInt(pos[0]));
	}
}
